<template>
  <div>
    <div class="tab_wrap">
      <van-tabs v-model="active" @click="onClick">
        <van-tab title="待检测"></van-tab>
        <van-tab title="初检不合格"></van-tab>
      </van-tabs>
      <div class="filter">
        <img
          src="@/assets/img/icon/icon_search.png"
          alt="搜索"
          @click="search"
        />
      </div>
    </div>

    <div class="page_space">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <template v-if="active === 0">
          <card-wrap
            v-for="(item, index) in list"
            :key="index"
            arrow
            class="mt-15"
            :title="item.detailInfoDTOS[0].businessVarietyName"
            :number="item.billNo"
            @click="toDetail(item.id, 'ToBeDetect')"
          >
            <card-item title="经营主体">{{
              item.intercourseEnterpriseName
            }}</card-item>
            <card-item title="所属市场">{{ item.belongEntName }}</card-item>
            <card-item title="采样日期">{{ item.billDate }}</card-item>
            <card-item title="采样数量">
              {{ item.detailInfoDTOS[0].num
              }}{{ item.detailInfoDTOS[0].packUnitName }}</card-item
            >
            <template #footer>
              <cardBtn
                bottomText="立即检测"
                @bottomClick="toDetection(item.id)"
              />
            </template>
          </card-wrap>
        </template>

        <template v-if="active === 1"> </template>
      </van-list>
    </div>
  </div>
</template>

<script>
import cardWrap from '@/components/base-card/wrap'
import cardItem from '@/components/base-card/item'
import cardBtn from '@/components/base-card/btn'
import { getBill, cancelBill, deleteDraft } from '@/api/bill'

export default {
  name: 'samplingTest',
  components: { cardWrap, cardItem, cardBtn },
  data() {
    return {
      loading: false,
      finished: false, // 触底加载结束
      list: [],
      params: {
        state: 'ToBeChecked',
        limit: 10,
        page: 1
      },
      value: '', // 搜索
      active: 0 // tabs
    }
  },
  created() {
    // 获取-搜索字
    if (this.$route.query.searchInfo) {
      let info = JSON.parse(decodeURIComponent(this.$route.query.searchInfo))
      console.log(info)
      this.params.page = 1
      this.list = []
      this.params = { ...this.params, ...info }
    }
  },
  methods: {
    search() {
      if (this.$route.query.searchInfo) {
        this.$router.replace('/sampling-test')
        let searchInfo = {
          intercourseEnterpriseNameLike: '',
          billDate: '',
          billNo: '',
          businessVarietyName: '',
          businessVarietyCode: ''
        }
        this.params.page = 1
        this.list = []
        this.params = { ...this.params, ...searchInfo }
        this.getBillList(this.params)
      } else {
        this.$router.push('/sampling-search?lastPage=samplingTest')
      }
    },
    // 触底加载
    onLoad() {
      this.getBillList(this.params)
    },
    // 去详情
    toDetail(id, state) {
      this.$router.push({
        name: 'samplingDetails',
        query: {
          id,
          state
        }
      })
    },
    // 删除草稿
    deleteDraft(id) {
      this.$dialog
        .alert({
          title: '是否删除?',
          showCancelButton: true
        })
        .then(async () => {
          const { code } = await deleteDraft(id)
          if (code === '0') {
            console.log('success')
            this.params.state = 'DRAFT'
            this.getBillList(this.params, true)
          } else {
            console.log('fail')
          }
        })
        .catch(() => {
          // on cancel
        })
    },
    // 立即检测
    toDetection(id) {
      this.$router.push({
        name: 'immediateDetection'
      })
    },
    // 切换tabs
    onClick(name) {
      let state = ''
      // 0 待检测 ToBeChecked 1 初检不合格 FirstUnqualified
      switch (name) {
        case 0:
          state = 'ToBeChecked'
          break
        case 1:
          state = 'FirstUnqualified'
          break
      }
      this.params.state = state
      this.getBillList(this.params, true)
    },
    // 获取采样台账列表
    async getBillList(params, isReset) {
      if (isReset) {
        this.loading = true
        this.finished = false
        this.list = []
        this.params.page = 1
      }
      const res = await getBill(params)
      if (res.code === '0') {
        res.data.content.forEach((item) => {
          this.list.push(item)
        })
        // 加载状态结束
        this.loading = false
        this.params.page++
        // 数据全部加载完成
        if (res.data.last) {
          this.finished = true
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.tab_wrap {
  display: flex;
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 999;

  .van-tabs {
    width: 100%;
  }

  .filter {
    width: 54px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    img {
      width: 20px;
      height: 20px;
    }
  }
}
</style>
